package it.univaq.disim.mwt.business.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import it.univaq.disim.mwt.business.TipologieNoteService;
import it.univaq.disim.mwt.business.model.TipologiaNotaDisciplinare;

public class JDBCTipologiaNoteService implements TipologieNoteService{

	TipologiaNotaDisciplinare tipologiaNoteDisciplinari=null;
	
	//FUNZIONA
	@Override
	public TipologiaNotaDisciplinare findTipologiaNoteById(Long id) {
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

	    String sql="select *from tipologie_note where id = ? ";
		
	    try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setLong(1, id);
			preparedStatement.executeQuery();
			resultSet = preparedStatement.getResultSet();
			resultSet.next();
			
			Long ide = resultSet.getLong("id");
			String tipologia = resultSet.getString("tipologia");
			tipologiaNoteDisciplinari = new TipologiaNotaDisciplinare(ide,tipologia);
			
			resultSet.close();
			preparedStatement.close();
			connection.close();
			
			return tipologiaNoteDisciplinari;
	
	    } catch (SQLException e) {
			
			e.printStackTrace();
		}
	   
		return null;
	}

	//FUNZIONA
	@Override
	public List<TipologiaNotaDisciplinare> findAllNoteDisciplinari() {
		
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		List<TipologiaNotaDisciplinare> listTipologieNote = new ArrayList<TipologiaNotaDisciplinare>();
		String sql = "select * from tipologie_note";
		TipologiaNotaDisciplinare tipologiaNoteDisciplinari = null;

		try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.executeQuery();
			resultSet = preparedStatement.getResultSet();
			
			while(resultSet.next()){
				

				Long ide = resultSet.getLong("id");
				String tipologia = resultSet.getString("tipologia");
				tipologiaNoteDisciplinari = new TipologiaNotaDisciplinare(ide,tipologia);
				
				listTipologieNote.add(tipologiaNoteDisciplinari);
			}
			
			resultSet.close();
			preparedStatement.close();
			connection.close();
			
			return listTipologieNote;
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}

		return null;
	}

}